<template>
  <ShareCard :loading="isLoading" :cardData="cardData" :showTitle="showTitle">
    <template #title>
      <div @click="handleGo">
        <Icon v-if="icon" :icon="icon" :size="26" class="mr-2" />{{ header }}</div
      >
    </template>
    <template v-if="dataSource?.length > 0">
      <div v-for="item in dataSource" :key="item.id" class="flex justify-between mb-2">
        <div
          class="overflow-hidden whitespace-nowrap overflow-ellipsis w-2/3 cursor-pointer"
          @click="handleView(item)"
        >
          <Tooltip :title="item['f.title']">
            {{ item['f.title'] }}
          </Tooltip>
        </div>
        <div
          class="overflow-hidden whitespace-nowrap overflow-ellipsis w-1/3 text-right"
          v-if="item['f.begin_date']"
        >
          <Tooltip :title="item['f.begin_date']">
            {{ item['f.begin_date'].substring(0, 8) }}
          </Tooltip>
        </div>
      </div>
    </template>
    <template v-else>
      <Empty :image="simpleImage" />
    </template>
    <ProcessShowDrawer @register="registerViewDrawer" />
  </ShareCard>
</template>
<script lang="ts" setup>
  import { ref, watch, onMounted } from 'vue';
  import { Card, Empty, Tooltip } from 'ant-design-vue';
  import { getFlowList } from '/@/api/process/process';
  import { useGo } from '/@/hooks/web/usePage';
  import Icon from '/@/components/Icon/index';
  import { useDrawer } from '/@/components/Drawer';
  import ProcessShowDrawer from '/@/views/pages/processManagement/processShowDrawer.vue';
  import ShareCard from './ShareCard.vue';
  const simpleImage = Empty.PRESENTED_IMAGE_SIMPLE;

  const props = defineProps({
    loading: Boolean,
    header: {
      type: String as PropType<string>,
      default: '',
    },
    width: {
      type: String as PropType<string>,
      default: '100%',
    },
    height: {
      type: String as PropType<string>,
      default: '300px',
    },
    icon: {
      type: String as PropType<string>,
      default: '',
    },
    pageSize: {
      type: Number,
      default: 6,
    },
    cardData: {
      type: Object as PropType<object>,
      default: () => {
        return {};
      },
    },
    showTitle: {
      type: Boolean,
      default: true,
    },
  });

  let flowOpStyle = 0;
  const isLoading = ref(false);
  const dataSource = ref<Recordable>([]);
  const fetch = () => {
    isLoading.value = true;
    getFlowList({ pageSize: props.pageSize, type: 'mine' })
      .then((res) => {
        dataSource.value = res.list || [];
        flowOpStyle = res.flowOpStyle;
      })
      .finally(() => {
        isLoading.value = false;
      });
  };
  watch(
    () => props.pageSize,
    () => {
      fetch();
    },
    {
      immediate: true,
    },
  );
  const go = useGo();
  const handleGo = () => {
    // 没有菜单项，故不能直接打开
    // go('/flow/mine');
    go({ path: '/flow/attend', query: { type: 'mine' } });
  };
  const [registerViewDrawer, { openDrawer: openViewDrawer }] = useDrawer();
  //查看详情
  const handleView = (record: object) => {
    console.log('record==>', record);
    if (flowOpStyle == 1) {
      let title = record['f.id'] + '-' + record['f.title'];
      if (title.length > 18) {
        title = title.substring(0, 18) + '...';
      }
      go({
        path: '/processShow',
        query: {
          flowId: record['f.id'],
          title: title,
          isFromProcess: true,
          cacheName: `processShow${record['f.id']}`,
        },
      });
    } else {
      openViewDrawer(true, {
        flowId: record['f.id'],
      });
    }
  };
  // watch(
  //   () => props.loading,
  //   () => {
  //     if (props.loading) {
  //       return;
  //     }
  //   },
  //   { immediate: true },
  // );
</script>
